﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Net;
using System.Net.Sockets;
using System.IO;
using System.Threading;

namespace AktionServer
{
    public class Monitor
    {
        private List<StreamWriter> streamWriters;
        Produkt produkt;

        public Monitor()
        {
            streamWriters = new List<StreamWriter>();	// Allokér liste til streamwriters
        }

        public void AddStreamWriter(StreamWriter streamWriter)
        {
            lock (this)
            {
                streamWriters.Add(streamWriter);
            }
        }

        public void RemoveStreamWriter(StreamWriter streamWriter)
        {
            lock (this)
            {
                streamWriters.Remove(streamWriter);
            }
        }

        public void BroadcastChat(string sidsteBud)
        {
            lock (this)
            {
                try
                {
                    foreach (StreamWriter streamWriter in streamWriters)		// Gennemløb aktive klienter
                    {
                        streamWriter.WriteLine(sidsteBud); // Broadcast
                        streamWriter.Flush();
                    }
                }
                catch (IOException e) { }  // ignorer
            }
        }
    }
}
